﻿using Batman.Maintenance.DTO.FormField;
using Batman.Maintenance.DTO.SearchBar;
using Batman.Maintenance.Enums;
using Batman.Maintenance.Utils;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Batman.Maintenance.DTO
{
    /// <summary>
    /// 字段和属性映射
    /// </summary>
    public class PropertyMap<T>
    {
        public string FieldName { get; set; }

        public Expression<Func<T, object>> Property { get; set; }

        public PropertyMap(string fieldName, Expression<Func<T, object>> expression)
        {
            FieldName = fieldName;
            Property = expression;
        }
    }

    public class MaintenanceSearchBar<T> : List<MaintenanceFormField>
    {
        public List<PropertyMap<T>> PropertyMaps { get; set; } = new List<PropertyMap<T>>();

        public MaintenanceFormField AddField(Expression<Func<T, object>> property, string label, MaintenanceFormFieldType type, MaintenanceSearchBarCondition condition)
        {

            MaintenanceFormField field = null;
            switch (type)
            {
                case MaintenanceFormFieldType.Text:
                    field = new MaintenanceTextFormField();
                    break;
                case MaintenanceFormFieldType.Checkbox:
                    field = new MaintenanceCheckboxFormField();
                    break;
                case MaintenanceFormFieldType.Selection:
                    field = new MaintenanceSelectionFormField();
                    break;
            }
            if (field == null) return null;
            this.Add(field);

            var fieldName = ExpressionUtil.GetSmallHumpWord(property.Body);
            field.FieldName = fieldName;
            field.Label = label;
            field.Condition = condition;

            this.PropertyMaps.Add(new PropertyMap<T>(fieldName, property));


            return field;
        }

        /// <summary>
        /// 添加选择框搜索
        /// </summary>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="property"></param>
        /// <param name="label"></param>
        /// <param name="condition"></param>
        /// <param name="options">选项</param>
        public void AddCheckboxField<TProperty>(Expression<Func<T, object>> property, string label, MaintenanceSearchBarCondition condition, List<MaintenanceLabelValueDTO> options)
        {
            var field = AddField(property, label, MaintenanceFormFieldType.Checkbox, condition) as MaintenanceCheckboxFormField;

            field.Options = options;
        }

        public void AddSelectionField(Expression<Func<T, object>> property, string label, List<MaintenanceLabelValueDTO> options, object defaultVal)
        {
            var field = AddField(property, label, MaintenanceFormFieldType.Selection, MaintenanceSearchBarCondition.等于) as MaintenanceSelectionFormField;

            field.Options = options;
            field.Val = defaultVal;
        }

        public void AddSelectionField(Expression<Func<T, object>> property, string label, SpecialSource source, object defaultVal = null)
        {
            var field = AddField(property, label, MaintenanceFormFieldType.Selection, MaintenanceSearchBarCondition.等于) as MaintenanceSelectionFormField;

            field.Source = source;
            field.Val = defaultVal;
        }
    }


}
